<template>
  <div class="rightForm">
    <baseTitle title="类型明细信息" />
    <rightContentForm ref="rightContentForm" />
    <div class="saveTool">
      <el-button type="primary" size="mini" :disabled="isDisabled" @click="saveForm">保存</el-button>
    </div>
    <baseTitle title="任务特殊属性" />
    <rightFormTable ref="rightFormTable" />
  </div>
</template>

<script>
import baseTitle from '@/components/baseTitle'
import rightContentForm from './components/rightContentForm'
import rightFormTable from '../rightForm/components/rightFormTable'
import { cloneDeep } from 'lodash'
import { saveTaskType } from '@/api/portalTaskUi/taskType'
import { isUndefined } from '@/util/util'
import { mapGetters } from 'vuex'
export default {
  name: 'RightForm',
  components: {
    baseTitle,
    rightContentForm,
    rightFormTable
  },
  data() {
    return {
      currentNode: {}, // 当前节点
      currentId: '' // 当前租户id
    }
  },
  computed: {
    isDisabled() {
      return Object.keys(this.currentNode).length <= 0
    },
    isAdd() {
      if (Object.keys(this.currentNode).length === 0) {
        return false
      } else {
        if (isUndefined(this.currentNode.isAdd)) {
          return true
        } else if (this.currentNode.isAdd) {
          return false
        }
      }
      return false
    },
    ...mapGetters(['user'])
  },

  created() {
    console.log(this.user)
    this.setCurrentId(this.user.id)
  },

  methods: {
    setCurrentId(id) {
      this.currentId = id
      // this.$refs.rightFormTable.setCurrentId(id);
    },
    setData(val) {
      this.currentNode = val
      this.$nextTick(() => {
        this.$refs.rightContentForm && this.$refs.rightContentForm.setData(val, this.currentId)
        this.$refs.rightFormTable && this.$refs.rightFormTable.setData(val)
      })
    },
    saveForm() {
      const data = this.$refs.rightContentForm.form
      const formData = cloneDeep(data)
      const targetArr = ['isAssess', 'isValid']
      for (const key in formData) {
        if (targetArr.includes(key)) {
          formData[key] = formData[key] === true ? '1' : '0'
        }
      }
      const params = {
        pid: this.currentNode.pid,
        id: !this.isAdd ? '' : this.currentNode.nodeId,
        tenantId: this.currentId
      }
      console.log(params)
      const query = Object.assign(formData, params)
      saveTaskType(query).then(res => {
        if (res.data.code === 0) {
          this.$message({
            message: '保存成功!',
            type: 'success'
          })
          // this.$set(this.currentNode, "isAdd", false)
          // this.$set(this.currentNode, "nodeId", res.data)
          this.$emit('save', this.currentNode)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.rightForm {
  width: calc(100% - 300px);
  height: 100%;
  .contentForm {
    padding-top: 10px;
  }
  .saveTool {
    text-align: right;
  }
}
</style>

